import Vue from 'vue'

const axios = Vue.$axios

const BlobDownload = (data, option, fileName) => {
    return new Promise((resolve, reject)=>{
        let href = window.URL.createObjectURL(new Blob([data], option))
        let a = document.createElement('a')
        a.style.display = 'none'
        a.href = href
        a.download = fileName
        let event = new MouseEvent('click')
        a.dispatchEvent(event)
        a.remove()
        resolve()
    })

}

export const UploadFileExcel = (data) => {
    var configs = {
        headers:{'Content-Type':'multipart/form-data'}
    };
    return axios.post("/batch/excel", data ,configs)
}

export const UploadFilePlain = (data, project) => {
    var configs = {
        headers:{'Content-Type':'multipart/form-data'}
    };
    return axios.post("/batch/plain?project="+project, data ,configs)
}

export const ExecuteBatch = (data) => {
    return axios({
        url: '/batch/execute',
        method: 'post',
        data: data
    })
}

export const DownloadBatch = (param, fileName) => {
    fileName = fileName || "batch.zip"
    return axios({
        url: '/batch/download',
        method: 'get',
        responseType: "blob",
        params: param
    }).then((res) => {
        BlobDownload(res.data, {type: "application/zip"}, fileName)
    })
}